<?php


namespace app\admin\model;


use think\Db;
use think\Exception;

class ServiceWeibaoModel extends BaseModel
{
    protected $name='service_weibao';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';


    public   function doAdd($data=[],$mw=[]){
        $type = intval($data['type']);
        $itype = intval($data['itype']);
        $total_price = $data['total_price'];
        $pay_id = 0;
        $this->startTrans();
        try {
            $insert = [
                'register_time' => time(),
                'deliver_time' => time(),
                'type' => $type,
                'business_type' => $type === 1 ? "常规维保" : ($type === 2 ? "石材维保" : ($type === 3 ? "墓碑换新" : "")),
                'business_num' => self::Sn(),
                'tomb_id' => $mw['tomb_id'],
                'tomb_num' => $mw['tomb_num'],
                'tomb_address' => $mw['tomb_region'],
                'add_user' => cmf_get_current_admin_id(),
                'handle_id' => cmf_get_current_admin_id(),
                'handle_name' => cmf_get_admin_name(),
                'service_status' => $itype==1?1:0,
                'pay_status' => 0,
                'customer_id' => $mw['customer_id'],
            ];
            $part_name = '';
            switch ($type) {
                case 1:
                    $con = $data['con'];
                    $is_part_id = array_column($con, 'part_id');
                    if (empty($is_part_id)){
                        $this->error = '请选择需要维保项目，至少有一项！';
                        return false;
                    }
                    $wb_id = $this->create($insert);
                    $wb_id = $wb_id['id'];
                    if ($itype==1){
                        $pay_id = OrderPayLogModel::doDataAdd( OrderPayLogModel::getselfPayNum('add_time','pay_sn','WB-'),
                            $wb_id,$mw['customer_id'], $mw['tomb_id'],$mw['tomb_num'],cmf_get_current_admin_id(),
                            $total_price,3,2,$mw['buyer_name'],$mw['buyer_phone']);
                        $pay_id = $pay_id['id'];
                    }
                    $part_name = '';
                    $part_price=0;
                    $free_warranty=0;
                    $total_price=0;
                    foreach ($con as $key => &$value) {
                        if (isset($value['part_id'])) {
                            if ($part_name){
                                $part_name=$part_name.','.$value['part_name'];
                            }else{
                                $part_name=$value['part_name'];
                            }
                            $value['type'] = 1;
                            $value['pay_id'] = $pay_id;
                            $value['wb_id'] = $wb_id;
                            $value['mw_id'] = $mw['tomb_id'];
                            $value['deliver_time'] = strtotime($value['deliver_time']);
                            $value['handle_id'] = cmf_get_current_admin_id();
                            $value['handle_name'] = cmf_get_admin_name();
                            $value['add_user'] = cmf_get_admin_name();
                            MaintenanceProjectModel::create($value);
                            $part_price+=$value['part_price'];
                            $free_warranty+=!empty($value['fee_reduction'])?$value['fee_reduction']:0;
                            $total_price+=$value['total_price'];
                        }
                    }
                    OrderPayLogModel::update(['tom_price'=>$part_price,'free_price'=>$free_warranty,'deal_price'=>$total_price,'total_price'=>$total_price,'actual_amount'=>$part_price],['id'=>$pay_id]);
                    $this->update(['business_content'=>$part_name],['id'=>$wb_id]);
                    break;
                case 2:
                    $part = $data['part'];
                    $is_part_id = array_column($part, 'part_id');
                    if (empty($is_part_id)){
                        $this->error = '请选择需要维保项目，至少有一项！';
                        return false;
                    }
                    $wb_id = $this->create($insert);
                    $wb_id = $wb_id['id'];
                    if ($itype==1) {
                        $pay_id = OrderPayLogModel::doDataAdd(OrderPayLogModel::getselfPayNum('add_time','pay_sn','WB-'), $wb_id, $mw['customer_id'],
                            $mw['tomb_id'], $mw['tomb_num'], cmf_get_current_admin_id(), $total_price, 3, 12,$mw['buyer_name'],$mw['buyer_phone']);
                        $pay_id = $pay_id['id'];
                    }
                    $part_price=0;
                    $free_warranty=0;
                    $total_price=0;
                    foreach ($part as $key => &$value) {
                        if (isset($value['part_id'])) {
                            if ($part_name){
                                $part_name=$part_name.','.$value['part_name'];
                            }else{
                                $part_name=$value['part_name'];
                            }
                            if (empty($value['deliver_time'])){
                                $this->error='请填写预计交付日期！';
                                return false;
                            }
                            $value['type'] = 2;
                            $value['wb_id'] = $wb_id;
                            $value['mw_id'] = $mw['tomb_id'];
                            $value['deliver_time'] = strtotime($value['deliver_time']);
                            $value['handle_id'] = cmf_get_current_admin_id();
                            $value['handle_name'] = cmf_get_admin_name();
                            $value['add_user'] = cmf_get_admin_name();
                            MaintenanceProjectModel::create($value);
                            $part_price+=$value['part_price'];
                            $free_warranty+=!empty($value['fee_reduction'])?$value['fee_reduction']:0;
                            $total_price+=$value['total_price'];
                        }
                    }
                    OrderPayLogModel::update(['tom_price'=>$part_price,'free_price'=>$free_warranty,'deal_price'=>$total_price,'total_price'=>$total_price,'actual_amount'=>$part_price],['id'=>$pay_id]);

                    $this->update(['business_content'=>$part_name],['id'=>$wb_id]);
                    break;
                case 3:
                    $array_mb= $data['mb'];
                    if (empty($array_mb['deliver_time'])){
                        $this->error='请填写预计交付日期！';
                        return false;
                    }
                    $wb_id = $this->create($insert);
                    $wb_id = $wb_id['id'];
                    if ($itype==1) {
                        $pay_id = OrderPayLogModel::doDataAdd(OrderPayLogModel::getselfPayNum('add_time','pay_sn','WB-'), $wb_id,
                            $mw['customer_id'], $mw['tomb_id'], $mw['tomb_num'], cmf_get_current_admin_id(), $total_price, 3, 11,$mw['buyer_name'],$mw['buyer_phone']);
                        $pay_id = $pay_id['id'];
                    }
                    $array_mb['type'] = 3;
                    $array_mb['wb_id'] = $wb_id;
                    $array_mb['mw_id'] = $mw['tomb_id'];
                    $array_mb['unit'] = '套';
                    $array_mb['warranty'] = 3;
                    $array_mb['deliver_time'] = strtotime($array_mb['deliver_time']);
                    $array_mb['handle_id'] = cmf_get_current_admin_id();
                    $array_mb['handle_name'] = cmf_get_admin_name();
                    $array_mb['add_user'] = cmf_get_admin_name();

                    MaintenanceProjectModel::create($array_mb);
                    $part_name='墓碑换新';
                    $part_price=$array_mb['part_price'];
                    $free_warranty=$array_mb['fee_reduction'];
                    $total_price=$array_mb['total_price'];
                    OrderPayLogModel::update(['tom_price'=>$part_price,'free_price'=>$free_warranty,'deal_price'=>$total_price,'total_price'=>$total_price,'actual_amount'=>$part_price],['id'=>$pay_id]);

                    $this->update(['business_content'=>$part_name],['id'=>$wb_id]);
                    break;
                default:
                    break;
            }
            $result = true;
            $this->commit();
        }catch(\Exception $e){
            $this->error='操作失败';
            $result = false;
            $this->rollback();
        }
        return $result;
    }
    public static function Sn(){
        $count = self::order('id DESC')->find();
        $time =date('Ym');
        if(date('Ym',$count['add_time'])==$time){
            $pact_num = 'WB-'.$time.'-'.str_pad(substr($count['business_num'],-4)+1, 4, '0', STR_PAD_LEFT);
        }else{
            $pact_num ='WB-'.$time.'-'.str_pad(1, 4, '0', STR_PAD_LEFT);;
        }
        return $pact_num;
    }
}